<template>
    <el-dialog title="填写计算说明" :visible.sync="visible" width="450px" append-to-body :close-on-click-modal="false">
        <el-form ref="dialogFormRef" :model="form" :rules="rules" label-width="100px" @submit.native.prevent>
            <el-form-item label="长度（m）" prop="length">
                <el-input v-model="form.length" type="number" clearable placeholder="请输入长度，可保留2位小数"></el-input>
            </el-form-item>
            <el-form-item label="宽度（m）" prop="width">
                <el-input v-model="form.width" type="number" clearable placeholder="请输入宽度，可保留2位小数"></el-input>
            </el-form-item>
            <el-form-item label="高度（m）" prop="height">
                <el-input v-model="form.height" type="number" clearable placeholder="请输入高度，可保留2位小数"></el-input>
            </el-form-item>
            <el-form-item>
                <span slot="label">面积m<sup>2</sup></span>
                <el-input v-model="form.area" disabled>
                    <template slot="append">
                        <span style="cursor: pointer;" @click="handleComputArea">生成面积</span>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <span slot="label">体积m<sup>3</sup></span>
                <el-input v-model="form.volume" disabled>
                    <template slot="append">
                        <span style="cursor: pointer;" @click="handleComputVolume">生成体积</span>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item v-if="form.type === '2'" label="请选择：" prop="id">
                <el-select v-model="form.id" clearable filterable placeholder="请选择需要复制的合同" style="width: 100%">
                    <el-option v-for="item in contractOptions" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <BaseButton type="second" @click="cancel">取消</BaseButton>
            <BaseButton :disabled="!form.length && !form.area && !form.volume" @click="handleSubmit">确认</BaseButton>
        </div>
    </el-dialog>
</template>

<script>
import { MaintenanceTaskModel } from '@/models'
import BigNumber from 'bignumber.js'
import { isNumber2 } from '@/utils/validate'

export default {
    name: 'ComputeDesDialog',
    data() {
        return {
            model: new MaintenanceTaskModel(),
            visible: false,
            submitLoading: false,
            form: {
                length: '',
                width: '',
                height: '',
                area: '',
                volume: '',
            },
            rules: {
                length: [
                    { required: true, message: '请输入', trigger: 'blur' },
                    { validator: this.validNum2, trigger: 'blur' }
                ],
                width: [{ validator: this.validNum2, trigger: 'blur' }],
                height: [{ validator: this.validNum2, trigger: 'blur' }],
            }
        }
    },
    methods: {
        // 取消按钮
        cancel() {
            this.visible = false
        },
        open(data) {
            this.visible = true
            this.form = {...data}
            // 重置下验证规则
            this.$nextTick(() => {
                this.$refs['dialogFormRef'].clearValidate()
            })
            // this.handleReset()
        },
        handleReset() {
            this.form = {
                length: '',
                width: '',
                height: '',
                area: '',
                volume: '',
            }
        },
        handleSubmit() {
            this.$emit('success', {...this.form})
            this.visible = false
        },
        handleComputArea() {
            if (!this.form.length && this.form.length !== 0) {
                this.$message.error('请输入长度')
                return
            }
            if (!this.form.width && this.form.width !== 0) {
                this.$message.error('请输入宽度')
                return
            }
            this.form.area = Number(new BigNumber(this.form.length).times(this.form.width))
        },
        handleComputVolume() {
            if (!this.form.length && this.form.length !== 0) {
                this.$message.error('请输入长度')
                return
            }
            if (!this.form.width && this.form.width !== 0) {
                this.$message.error('请输入宽度')
                return
            }
            if (!this.form.height && this.form.height !== 0) {
                this.$message.error('请输入高度')
                return
            }
            this.form.volume = Number(new BigNumber(this.form.length).times(this.form.width).times(this.form.height))
        },
        validNum2(rule, value, callback) {
            if (value && !isNumber2(value)) {
                callback(new Error('填写格式错误'))
            } else {
                callback()
            }
        },
    }
}
</script>

<style lang="scss" scoped>
.w-300 {
    width: 300px;
}
</style>
